package com.hjj.springboot.config;

import com.hjj.springboot.interceptor.SysInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebAppConfigurer implements WebMvcConfigurer {

//    跨域问题
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOriginPatterns("*")
                .allowCredentials(true)
                .allowedMethods("GET", "HEAD", "POST", "PUT", "DELETE","OPTIONS")
                .maxAge(3600);
    }

//  图片映射
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/img/swiper/**").addResourceLocations("file:D:\\graduation-project\\graduation-project-img\\swiperImgs\\");
        registry.addResourceHandler("/img/bigType/**").addResourceLocations("file:D:\\graduation-project\\graduation-project-img\\bigTypeImgs\\");
        registry.addResourceHandler("/img/product/**").addResourceLocations("file:D:\\graduation-project\\graduation-project-img\\productImgs\\");
        registry.addResourceHandler("/img/productSwiperImgs/**").addResourceLocations("file:D:\\graduation-project\\graduation-project-img\\productSwiperImgs\\");
        registry.addResourceHandler("/img/productIntroImgs/**").addResourceLocations("file:D:\\graduation-project\\graduation-project-img\\productIntroImgs\\");
        registry.addResourceHandler("/img/avatarUrl/**").addResourceLocations("file:D:\\graduation-project\\graduation-project-img\\avatarUrlImgs\\");
        registry.addResourceHandler("/img/talkImgs/**").addResourceLocations("file:D:\\graduation-project\\graduation-project-img\\talkImgs\\");
    }

//    鉴权拦截
    @Bean
    public SysInterceptor sysInterceptor() {
        return new SysInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        String[] patterns=new String[]{"/api/**","/admin/**/export","/adminLogin","/adminRegister","/product/**","/bigType/**","/user/**","/weixinpay/**","/comment/**","/promble/**","/tips/**","/talk/**","/notice/**","/search/**","/rider/user/login","/rider/user/register"};
        registry.addInterceptor(sysInterceptor())
                .addPathPatterns("/**")
                .excludePathPatterns(patterns);

    }


    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
